
<?php 
require 'Model.php';
class CartoonModel extends Model{
	function getCartoonList(){
		$sql="select * from cartoons";		
		$data=$this->conn->query($sql);
		return $data;
	}
	function delCartoon($cid){
		$sql="delete from cartoons where cid=$cid";
		$result=$this->conn->query($sql);
		return $result;
	}
	function addcartoon($data,$file){
			//文件上传
			$arr=explode('.',$file['pic']['name']);//拆分成数组
			$suffix=$arr[count($arr)-1];//count($arr)为数组长度
			$newname=date("YmdHis").rand(100,999).'.'.$suffix;//newname代表上传图片新名字
			$filepath='./posters/';

			move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
			//把数据插入数据库
			$cartoonname=$data['cartoonname'];
			$tid=$data['tid'];
			$pic=$newname;
			$intro=$data['intro'];
			$address=$data['address'];
			$sql="insert into cartoons value(null,'$cartoonname',$tid,'$pic','$intro',now(),2,0,0,'$address')";
			$result=$this->conn->query($sql);
			return $result;

		}

	function editcartoon($cid){
		$sql="select * from cartoons where cid=$cid";
		$data=$this->conn->query($sql);
		return $data;//一条数据

	}
function updatecartoon($data,$file){
        $cid=$data['cid'];
        $cartoonname=$data['cartoonname'];
		$tid=$data['tid'];
		$intro=$data['intro'];
		$address=$data['address'];
		//接收数据
		if($file['pic']['size']==0){
			//没上传新头像
			$sql="update cartoons set cartoonname='$cartoonname',tid=$tid,intro='$intro',address='$address'where cid=$cid ";
		}
		else{
        $arr=explode('.',$file['pic']['name']);
		$suffix=$arry[count($arr)-1];//后缀名
		$newname=date("YmdHis").rand(100,999).'.'.$suffix;//文件新名字
		$filepath='./posters/';		move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
		$sql="update cartoons set cartoonname='$cartoonname',tid=$tid,intro='$intro',address='$address',pic='$newname' where cid=$cid ";
			//上传头像并且根据cid更新数据
		}
//如果修改了头像，需要上传新头像，否则原头像不动
       $result=$this->conn->query($sql);
		return $result;
		
	}
	function searchcartoon($cartoonname){
	$key=trim($_POST['key']);
    $sql="select * from cartoons  where (cartoonname like'%$key%')";
    $data=$this->conn->query($sql);
	return $data;
   }








	}

 ?>	
